<?php class Order extends CI_Controller {

    function __construct() {
        //TODO
    }

    public function orderForm() {
        //TODO
    }

    public function orderCodeSearch() {
        $view = 'orderCodeSearch';
        $mainResult['attributes'] = array('class' => 'orderCodeSearchForm');
        $result = $this->userformvalidation->orderCodeSearchFormValidation(); //주문 확인 폼 검증 라이브러리

        if($result != false) { //검증 성공시
            $sql = "select order_code, order_password from orders
                    where order_code=".mysql_real_escape_string($result['order_code']); //주문 코드 검색
            $query = $this->db->query($sql);
            $passwordValidation = $query->result();

            if($query->num_rows() <= 0) { //주문 코드가 없을 시
                echo("<script>
                     alert('Not found order code');
                     window.location='".base_url()."user/order/orderCodeSearch';
                     </script>");
                return;
            } else {
                foreach($passwordValidation as $row):
                    if($row->order_password == $result['order_password']) { //주문 코드의 비밀번호 match 성공시
                        redirect('user/order/orderView/code/'.$result['order_code']);
                        return;
                    } else { //주문 코드의 비밀번호 match 실패시
                        echo("<script>
                             alert('Not match password');
                             window.location='".base_url()."user/order/orderCodeSearch';
                             </script>");
                        return;
                    }
                endforeach;
            }
        }

        $this->shoplayout->userLayout($mainResult, $view, true);
        return;
    }

    public function orderView() {
        $parameter = $this->uri->ruri_to_assoc(3, array('code')); //주문 코드 파싱
        $code = $parameter['code'] ? $parameter['code'] : 0;
        $view = 'orderView';
        $mainResult['code'] = $code;
        $sql = "select * from orders
                where order_code=".mysql_real_escape_string($code); //주문 코드 검색
        $query = $this->db->query($sql);
        $mainResult['orderResult'] = $query->result();
        $sql = "select DISTINCT delivery_code, delivery_date, history
                from order_items
                where order_code=".mysql_real_escape_string($code); //주문 코드의 상품 배송 검색
        $query = $this->db->query($sql);
        $mainResult['deliveryResult'] = $query->result();
        $sql = "select items.item_code, items.thumbnail, items.item_name, order_items.item_quantity, order_items.sub_price, order_items.delivery_code
                from order_items, items
                where order_items.item_code=items.item_code and order_items.order_code=".mysql_real_escape_string($code); //주문 코드의 상품 검색
        $query = $this->db->query($sql);
        $mainResult['itemResult'] = $query->result();
        $mainResult['attributes'] = array('class' => 'orderViewForm');

        $this->shoplayout->userLayout($mainResult, $view, null);
        return;
    }
}